package com.carrental.crm.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carrental.common.result.Result;
import com.carrental.crm.dto.OrderQueryDTO;
import com.carrental.crm.service.OrderService;
import com.carrental.crm.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 订单控制器
 */
@Slf4j
@Api(tags = "订单管理")
@RestController
@RequestMapping("/order")
@RequiredArgsConstructor
public class OrderController {

    private final OrderService orderService;

    @ApiOperation("创建订单")
    @PostMapping("/create")
    public Result<Boolean> create(@RequestBody Map<String, Object> params) {
        log.info("创建订单，参数：{}", params);
        boolean result = orderService.createOrder(params);
        return Result.success(result);
    }

    @ApiOperation("查询我的订单")
    @PostMapping("/my/list")
    public Result<Page<OrderVO>> getMyOrders(@RequestBody OrderQueryDTO queryDTO) {
        log.info("查询我的订单，参数：{}", queryDTO);
        Page<OrderVO> page = orderService.pageMyOrders(queryDTO);
        return Result.success(page);
    }

    @ApiOperation("订单详情")
    @GetMapping("/detail/{orderNo}")
    public Result<OrderVO> getOrderDetail(@PathVariable String orderNo) {
        log.info("查询订单详情，订单号：{}", orderNo);
        OrderVO orderVO = orderService.getOrderDetail(orderNo);
        return Result.success(orderVO);
    }

    @ApiOperation("取消订单")
    @PostMapping("/cancel/{orderNo}")
    public Result<Boolean> cancelOrder(@PathVariable String orderNo) {
        log.info("取消订单，订单号：{}", orderNo);
        boolean result = orderService.cancelOrder(orderNo);
        return Result.success(result);
    }

    @ApiOperation("支付订单")
    @PostMapping("/pay")
    public Result<Boolean> payOrder(@RequestBody Map<String, Object> params) {
        String orderNo = (String) params.get("orderNo");
        Integer payType = (Integer) params.get("payType");
        log.info("支付订单，订单号：{}，支付方式：{}", orderNo, payType);
        boolean result = orderService.payOrder(orderNo, payType);
        return Result.success(result);
    }
    @ApiOperation("支付订单(路径参数)")
    @PostMapping("/pay/{orderNo}")
    public Result<Boolean> payOrderByPath(@PathVariable String orderNo, @RequestBody Map<String, Object> params) {
        Integer payType = (Integer) params.get("payType");
        log.info("支付订单(路径参数)，订单号：{}，支付方式：{}", orderNo, payType);
        boolean result = orderService.payOrder(orderNo, payType);
        return Result.success(result);
    }
}